#!/usr/bin/env python
# -*- conding:utf-8 -*-
# __Author__:'liudong'
import pickle, os,time,logging

all_user_info = {}  # 保存文件所有用户信息


def add_user():
    old_file = open('..\\db\\user.db', 'rb')
    all_user_info = pickle.load(old_file)
    old_file.close()
    add_username = input('please input username you want add.')
    other_information = input('please input basic information, like this: '  # 输入的内容是字符串
                              """
                              card_num, #信用卡号
                              password,  #密码
                              15000,  # 信用卡额度
                              15000,  # 本月可用额度
                              0,  # 储蓄金额
                              2016-01-01,  # 注册时间
                              2021-01-01,  # 到期日
                              0,  #状态 0=normal,1=locked
                              """)
    list_other_information = list(other_information.split(','))
    # print(list_other_information, type(list_other_information))
    all_user_info[add_username] = list_other_information
    # print(add_user_key, type(add_user_key))
    user_card_no = all_user_info[add_username][0]  # 获取用户卡号
    # print(user_card_no)
    user_db_file = open('..\\db\\usernew.db', 'wb')
    pickle.dump(all_user_info, user_db_file)
    user_db_file.close()
    os.remove('..\\db\\user.db')
    os.renames('..\\db\\usernew.db', '..\\db\\user.db')
    os.chdir('..\\db')  # 切换到db目录
    os.mkdir(user_card_no)  # 创建该用户卡号为名称的文件夹
    print('%s user created' % add_username)
    logging.basicConfig(filename='timelog.txt', level=logging.INFO, format='%(asctime)s%(message)s',
                        datefmt='%m/%d/%Y %I:%M:%S %p')
    logging.info('add user %s' % add_username + '\n')                #记录用户增加日志


def del_user():
    print('_' * 50)
    del_username = input('please input username you want to delete.')
    confirm = input('please confirm? y[Y]')
    if confirm == 'y' or confirm == 'Y':
        os.chdir('..\\db\\')
        del_user_file = open('..\\DB\\user.db', 'rb')  # 打开用户基本信息文件
        del_uesr_list = pickle.load(del_user_file)  # 读取文件所有用户基本信息到del_user_list
        del_user_file.close()
        dir_del = str(del_uesr_list[del_username][0])  # 保存要删除用户目录的名字
        del del_uesr_list[del_username]  # 从字典del_user_list中删除用户输入的用户名为key的所有信息
        os.rmdir(dir_del)  # 删除目录
        # print(del_uesr_list,type(del_uesr_list))
        new_file = open('..\\DB\\usernew.db', 'wb')  # 打开新的文件用于写入新用户字典信息
        pickle.dump(del_uesr_list, new_file)  # 将新的字典信息序列化后写入usernew.db文件
        new_file.close()  # 关闭文件
        os.remove('..\\db\\user.db')  # 将原文件删除
        os.renames('..\\db\\usernew.db', '..\\db\\user.db')
        logging.basicConfig(filename='timelog.txt', level=logging.INFO, format='%(asctime)s%(message)s',
                            datefmt='%m/%d/%Y %I:%M:%S %p')
        logging.info('delete user %s' % del_username + '\n')

def lock_user():
    print('_' * 50)
    lock_username = input('please input username you want to delete.')
    confirm = input('please confirm? y[Y]')
    if confirm == 'y' or confirm == 'Y':
        os.chdir('..\\db\\')
        lock_user_file = open('..\\DB\\user.db', 'rb')  # 打开用户基本信息文件
        lock_user_list = pickle.load(lock_user_file)  # 读取文件所有用户基本信息到del_user_list
        lock_user_file.close()
        lock_user_list[lock_username][7] = '1'
        new_file = open('..\\DB\\usernew.db', 'wb')  # 打开新的文件用于写入新用户字典信息
        pickle.dump(lock_uesr_list, new_file)  # 将新的字典信息序列化后写入usernew.db文件
        new_file.close()  # 关闭文件
        os.remove('..\\db\\user.db')  # 将原文件删除
        os.renames('..\\db\\usernew.db', '..\\db\\user.db')  #新文件改名
        logging.basicConfig(filename='timelog.txt', level=logging.INFO, format='%(asctime)s%(message)s',
                            datefmt='%m/%d/%Y %I:%M:%S %p')
        logging.info('lock user %s' % lock_username + '\n')